导航菜单
首页 >  linux shell命令行及脚本编程实例详解期末考试题  > linux必会的30道shell编程面试题及讲解

linux必会的30道shell编程面试题及讲解

linux必会的30道shell编程面试题及讲解 1.通过ansibe批量给主机修改名称 #!/bin/bashcat /etc/Hostname|\while read linedo Host_IP="$(echo $line| awk '{print $1}')" Host_name="$(echo $line| awk '{print $2}')" ansible $Host_IP -m hostname -a "name=$Host_name"done 2.

使用for循环在/CSDN目录下通过随机小写10个字母加固定字符串CSDN批量创建10个html文件,名称例如为:

#coaolvajcq_CSDN.html qnvuxvicni_CSDN.html vioesjmcbu_CSDN.html#gmkhrancxh_CSDN.html tmdjormaxr_CSDN.html wzewnojiwe_CSDN.html 实现过程 #!/bin/bashdir=/CSDNfor i in {1..10}do # 第一种获取随机字符方法:date毫秒方式 suiji=`date +%N%s|md5sum|cut -c1-10|tr '0-9' 'a-z'`# 第二种获取随机字符方法:tr,不是a-z的都排除,不可以用cut命令,会卡住 suiji=`tr -cd 'a-z' /dev/null#取8个字符串passwd=`cat /dev/urandom |head |md5sum | head -c8`#账户密码存放echo CSDN$i $passwd >> /CSDN.txtecho $passwd | passwd --stdin CSDN$i >/dev/nullif [ $? -eq 0 ];thenecho 用户CSDN${i}创建成功elseecho 用户CSDN${i}创建失败fidone 5.

写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)

实现过程 #!/bin/bash. /etc/init.d/functionsfor ip in 10.0.0.{1..254}do ping -c1 -i1 -W1 $ip &>/dev/null# 次数 # 间隔 # 超时时间 if [ $? -eq 0 ] then action "$ip is ok" /bin/true else action "$ip is failed" /bin/falsefidone#第二种方法#!/bin/bashnmap -sn 10.0.0.0/24 6.

写一个脚本解决DOS生产案例提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。

[root@CSDN-m01 ~]# echo 3 119.121.180.245 3 119.121.180.245 [root@CSDN-m01 ~]# echo 3 119.121.180.245 |{ read count ip ; echo $count $ip ; } 3 119.121.180.245 #第一种#!/bin/bash 1234567netstat_file=/root/connect.txt awk -F'[ :]+' '/^tcp/ && $6!~/0.0.0.0/ {print $6}' /server/files/netstat.log |sort |uniq -c |sort - rn|head >$netstat_file#1.取出每个ip的次数 while read count ip do ip_drop_cnt=`iptables -nL |grep -wc "$ip"` #2.读取文件每一行并进行判断 if [ $count -ge 5 ] && [ $ip_drop_cnt -eq 0 ] then iptables -I INPUT -s $ip -j DROP fi # 访问数量>=5 并且 防火墙中没有存在这个拒绝的规则 那么 iptables drop done>/tmp/droplist.logfidone$log add_iptables sleep 180done} main8.

如何实现对MySQL数据库进行分库备份,请用脚本实现

实现过程 # 思路:先熟悉不进入数据库查看数据库中库的命令:mysql -uroot -pCSDN123 -e xxxxx然后尝试现将一个库进行备份操作,获取到所有库名称,然后进行循环操作#!/bin/bashfor dbname in `mysql -e 'show databases;'|egrep -v 'schema|test|Database'` # 这三个名称是排除出去的,前两个目前不深入研究,不是自己创建的库,第三个是标题do mysqldump -B $dbname |gzip >/root/$dbname.sql.gzdone# 测试gzip -d /root/$dbname.sql.gz 9.

如何实现对MySQL数据库进行分库加分表备份,请用脚本实现

#!/bin/bashuser=rootpass=CSDN123mysql_cmd="mysql -u$user -p$pass"for db in `$mysql_cmd -e "show databases;"|egrep -v "Database|schema"`do for table in `$mysql_cmd -e "show tables from $db;"|egrep -v "Tables_in"` domysqldump -u$user -p$pass $db $table|gzip >/backup/${db}_${table}.sql.gz donedone 10.

bash for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。II am CSDN teacher welcome to CSDN training class.

实现过程 #!/bin/bashstr="I am CSDN teacher welcome to CSDN training class."for zifu in $strdo if [ ${#zifu} -gt 6 ] then echo $zifu ${#zifu} fidone# 方法2 awk方式echo "I am CSDN teacher welcome to CSDN training class."|awk '{for(i=1;i6 )print $i}' 11.

开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。

实现过程 # 传参方式#!/bin/bashnum1=$1num2=$2if [ $# -ne 2 ]thenecho "请输入两个参数"exit 2fiexpr $num1 + $num2 + 666 &>/dev/nullif [ $? -eq 0 ]thenif [ $num1 -gt $num2 ]thenecho "$num1 > $num2"elif [ $num1 -lt $num2 ]thenecho "$num1 < $num2"elseecho "$num1 = $num2"fielseecho "请输入纯数字"fi# read方式#!/bin/bashread -p "请输入第一个数字" num1read -p "请输入第二个数字" num2expr $num1 + $num2 + 666 &>/dev/nullif [ $? -eq 0 ]thenif [ $num1 -gt $num2 ]thenecho "$num1 > $num2"elif [ $num1 -lt $num2 ]thenecho "$num1 < $num2"elseecho "$num1 = $num2"fielseecho "请输入纯数字"fi 12.

打印选择菜单,一键安装Web服务:

[root@CSDNscripts]# sh menu.sh

1.[install lamp]

2.[install lnmp]

3.[exit]

pls input the num you want:

要求:

1、当用户输入1时,输出“startinstalling lamp.”然后执

行/server/scripts/lamp.sh,脚本内容输出“lampis installed”后退出脚本;

2、当用户输入2时,输出“startinstalling lnmp.”然后执

行/server/scripts/lnmp.sh输出“lnmpis installed”后退出脚本;

3、当输入3时,退出当前菜单及脚本;

4、当输入任何其它字符,给出提示“Input error”后退出脚本。

5、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行

等。

#!/bin/bashread -p "input your choose" commandlamp() {echo “startinstalling lamp.”[ -f /server/scripts/lamp.sh ] && [ -x /server/scripts/lamp.sh ]thensh /server/scripts/lamp.shecho "lampis installed"elseecho "can not running/server/scripts/lamp.sh"exit 1}lnmp() {echo "startinstalling lnmp."[ -f /server/scripts/lnmp.sh ] && [ -x /server/scripts/lnmp.sh ]thensh /server/scripts/lnmp.shthenecho "lnmpis installed"elseecho "can not running/server/scripts/lnmp.sh"exit 2}exit() {exit 3}case $command in1)lamp;2)lnmp;3)exit;*)echo "Input error"exit 4esac 13.

1、监控web服务是否正常,不低于3种监控策略。

2、监控db服务是否正常,不低于3种监控策略。

要求间隔1分钟,持续监控。

1.监控web: 1、curl方式 2、wget 进程和端口 进程 ps -ef 或 ps -C nginx -o pid comm --noheaders top:M 按内存使用率排序 P 按cpu使用率排序(默认) q 退出 z 开启/关闭颜色 x 显示当前按照哪列排序 top -p 46648 top -b 进入批处理模式 可以搭配管道[root@CSDN-m01 ~]# top -b -p 46649 | awk '/nginx/' 46649 nginx 20 0 125496 3600 1056 S 0.0 0.2 0:00.01 nginx[root@CSDN-m01 ~]# top -b -p 46649 | awk '/nginx/{print $1,$NF,$(NF-3)}' 46649 nginx 0.0 端口 ss -lntup lsof -i nmap nginx状态模块 stub_status;2.监控db 端口、进程、执行sql语句 ----> curl和wget及相关参数 curl - I:只显示响应头信息- H: 修改请求头信息- A: 指定浏览器(客户端代理)- v: 显示详细请求和相应信息- L: 跟随跳转- s: 不显示头部统计信息,配合管道或-o- o: 把curl的输出写入到指定文件- u :指定用于登录的用户名和密码curl -w "%{http_code}\n" -so /dev/null 10.0.0.61 wget - O: 下载到指定文件中- P: 下载到指定目录中(目录不存在自动创建)-- debug: 显示请求和响应详细过程- t: 最大尝试次数- T: 超时时间- q: 静默模式,不显示下载过程-- spider: 爬虫,只访问不下载 14.(需要多加熟悉)

监控memcache服务是否正常,模拟用户(web客户端)检测。

使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率。

[root@CSDN-m01 ~]# rpm -ql memcached /etc/sysconfig/memcached /usr/bin/memcached 123▽root@CSDN-m01 ~]# vim /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="-l 172.16.1.61" [root@CSDN-m01 ~]# [root@CSDN-m01 ~]# systemctl restart memcached.service [root@CSDN-m01 ~]# ps -ef |grep mem memcach+ 51127 1 0 11:45 ? 00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 -l 172.16.1.61 root 51136 41363 0 11:45 pts/0 00:00:00 grep --color=auto mem [root@CSDN-m01 ~]# telnet 10.0.0.61 11211 Trying 10.0.0.61... telnet: connect to address 10.0.0.61: Connection refused [root@CSDN-m01 ~]# telnet 172.16.1.61 11211 Trying 172.16.1.61... Connected to 172.16.1.61. Escape character is '^]'. ERROR ERROR ERROR ^]telnet> Connection closed. #wordpress缓存数据缓存到memcached中: https://cn.wordpress.org/plugins/memcached/ #wordpress缓存数据缓存到redis中: https://cn.wordpress.org/plugins/redis-cache/ #wordpress会自动检查wp-content下面是否有object-cache.php 使用memcached缓存wordpress博文数据 修改:array('127.0.0.1','');为memcached服务器ip地址[root@CSDN-m01 ~]# printf "stats\r\n"|nc 172.16.1.61 11211 |awk '/cmd_get/{get=$3}/get_hits/{hit=$3}END{print hit/get}' 0.5 15.

面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次。

#!/bin/bash#file=`find /var/html/www -type f|xargs md5sum >/root/filemd5.txt` # 文件和MD5md5sum=`md5sum -c /root/filemd5.txt &>/root/webcheck.txt`for i in `awk '{print $2}' /root/webcheck.txt`do if [ "$i" = "FAILED" ]thenawk '/FAILED/{print $0}' /root/webcheck.txtelseecho "ok"fidone 案例注意事项:md5sum排除正常情况下会经常变动的目录:如日志目录md5sum使用时间:在每次正常代码上线之后 其他思路可使用find -mmin -5最近五分钟内修改的内容 # 找出目录下的文件find /var/html/www -type f# 生成指纹时排除经常变动的文件find /var/html/www -type f | egrep -wv "cache|uploads" | xargs md5sum >/backup/www.md5# 将文件及对应指纹输出到另一个文件中find /var/html/www -type f | xargs md5sum >/root/www.md5# 对指纹文件对应关系进行检查md5sum -c /root/www.md5# awk方式过滤出异常文件名输出到另一个文件中,用于发送邮件md5sum --quiet -c /root/www.md5 | awk '/FAILED/{print $1}'# 检查异常文件中是否存在内容,有则发送邮件[ -s /web_eeor.txt ] && { mail -s "网站文件被修改" @qq.com /backup/www.md5md5sum --quiet -c /root/www.md5 2>/dev/null | awk '/FAILED/{print $1}' >/web_error.txt[ -s /web_eeor.txt ] && { mail -s "网站文件被修改" 2462560493@qq.com >/root$direlseecho "数字重复" && continuefi}main() {checkjilu}while truedo maindone18.

已知下面的字符串是通过RANDOM随机数变量md5sum后,再截取一部分连续字符串的结果,请破解这些字符串对应的使用md5sum处理前的RANDOM对应的数字?

实现过程 #!/bin/bashnull=(210292900205d1ca3da16771f6d12dd) for num in {1..32767}do num_md5=`echo "$num"|md5sum|cut -c1-8`#echo $num $num_md5for null in ${null[*]}do if [ "$num_md5" = "$null" ] then echo md5值:$null 原数字:$num break fidonedone案例 # 将0-32767分别生成md5sum存入到文件中# 过滤题目中的md5sumfor n in {0..32767}do md5=`echo $n|md5sum` echo $md5 $n >>/root/md5.txtdone grep '题目中md5值' /root/md5.txt 19.

批量检查多个网站地址是否正常,要求:shell数组方法实现,检测策略尽量模拟用户访问思路

实现过程 #!/bin/bash#http://www.etiantian.org#http://www.taobao.com#http://CSDN.blog.51cto.com#http://10.0.0.7 . /etc/init.d/functionsurl=( `cat /root/url.txt` )# 将url添加到文件中for n in ${url[*]}do curl -q $n >/dev/null if [ $? -eq 0 ] then action "$n is ok" /bin/true else action "$n is failed" /bin/false fidone######亲自测试已成功,可以再次完善######[root@m01 CSDN]# sh /server/scripts/url_shuzu_test.shhttp://www.etiantian.org/index.html is okhttp://www.etiantian.org/1.html is okhttp://post.etiantian.org/index.html is failedhttp://mp3.etiantian.org/index.html is failedhttp://www.etiantian.org/3.html is okhttp://post.etiantian.org/2.html is failed 案例 #!/bin/bash url_list=( http://www.etiantian.org http://www.taobao.com oldray.ren http://CSDN.blog.51cto.com http://10.0.0.7 ). /etc/init.d/functions #[[ ! $url =~ [0-Z]+\.(org|com|cn|xyz)$ ]] check() { url=$1[[ $url =~ [0-Z]+\.(org|com|cn|xyz)$ || $url =~ [0-9.]+$ ]] || { echo "Usage: $0 $url" continue } }chk_url() { url=$1 wget -q -t 3 -T 1 --spider $url if [ $? -eq 0 ] thenaction "$url is ok" /bin/true elseaction "$url is failed" /bin/false fi}main() { for url in ${url_list[*]} do check $url chk_url $url done }main 20.

用shell处理以下内容

1、按单词出现频率降序排序!

2、按字母出现频率降序排序!

the squid project provides a number ofresources to assist users design,implement and support squid installations.Please browse the documentation and support sections for more infomation,byCSDN training.

str="the squid project provides a number ofresources to assist users design,implement and support squid installations.Please browse the documentation and support sections for more infomation,byCSDN training."echo "$str"|grep "[a-Z]" -o|sort|uniq -c|sort -rnk1 echoecho "$str"|egrep "[a-Z]+" -o|sort|uniq -c|sort -rnk1 案例 # 方法1:egrep进行实现egrep '[a-Z]+' -o /server/files/word.txt|sort |uniq -c |sort -rn# 方法2:egrep+awk数组进行实现egrep '[a-Z]+' -o /server/files/word.txt|awk 'arr[$0]++END{for(n in arr)print n,arr[n]}'|sort -rnk2# 方法3:RS+awk数组进行实现awk -vRS='[ .,\n]+' 'arr[$0]++END{for(n in arr)print n,arr[n]}' /server/files/word.txt# 以空格、,、.、回车为换行符,遇到这些符号就换行,最后输出的就全部是单词了# 方法4:awk -F进行实现awk -F'[ .,]+' '{for(i=1;i

相关推荐: